#!/usr/bin/env ruby
# This program anaylzes the profile file from arnetminer. 
# Please refer to wiki for the profile file format and sample profile file. 
# This program outputs hash table <key, value> pairs where key is
# the tag and value is the content.  

# input: file name from command line
# sample file: profile.txt
# [name]G.W.[lastname]bush[/lastname][/name][writer]\n
# w[/writer]
# [name] bla[/name]
# output: hash table <key, value> pairs where key is the tag and value is the content.  

# usage:generate-profile.rb profile.txt

profile=IO.read(ARGV[0])

$tag_hash=Hash.new

def Collect_Tag(str)
	regex=Regexp.new(/\[(.+?)\](.*?)\[\/\1\]/mi)
	str.scan(regex)do	|x,y| 
		if(y.match(regex))
			Collect_Tag(y)
		end
		y=y.gsub(/\[.*?\]/, '')
		$tag_hash[x]=$tag_hash[x].to_s+y.to_s	
	end
end

Collect_Tag(profile)

$tag_hash.each do |key, value|
	puts "#{key}: #{value}\n\n"
end
